package org.example.base;

import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

public class ReferenceTest {

    public static String s = "345";

    public static void main(String[] args) {
        Object o = new Object();
        ReferenceQueue<Object> queue = new ReferenceQueue<Object>();
        PhantomReference<Object> a = new PhantomReference<Object>(o, queue);
//        System.out.println("queue = " + queue.poll());
//        System.out.println("o.hashCode() = " + o.hashCode());

        o = null;
        System.gc();
        System.out.println("queue = " + queue.poll());

        System.out.println(s.hashCode());
        f(s);

        System.out.println("s = " + s);


        Cat cat = new Cat();
        cat.setName("222");
        f2(cat);

        System.out.println(cat.getName());
        f3(cat);
        System.out.println("cat.getName() = " + cat.getName());
    }

    public static void f(String s2) {
        System.out.println("s2.hashCode() = " + s2.hashCode());
        s2 = "123";
    }

    public static void f2(Cat cat) {
        cat.setName("111");
    }

    public static void f3(Cat cat) {
        cat = new Cat();
        cat.setName("555");
    }
}

class Cat {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
